package study.web.servlet.old;

import com.alibaba.fastjson.JSON;
import study.pojo.Brand;
import study.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

//@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    private BrandServiceImpl service=new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理POST请求的乱码问题
        req.setCharacterEncoding("utf-8");

        //1.接受品牌数据
        BufferedReader br = req.getReader();
        String params=br.readLine();

        //2.转换为Brand对象
        Brand brand = JSON.parseObject(params,Brand.class);

        //2.调用service完成添加
        service.add(brand);

        //3.响应成功的表示
        resp.getWriter().write("success");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        this.doGet(req,resp);
    }
}
